/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: Main
 * Author:   AEO WUGUOZHI
 * Date:     2020/7/17 17:42
 * Description: none
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.imooc.Test;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 〈一句话功能简述〉<br> 
 * 〈none〉
 *
 * @author AEO WUGUOZHI
 * @create 2020/7/17
 * @since 1.0.0
 */
public class Main {

    public static void main(String[] args) throws Exception{
        Book book1 = new Book("红楼梦",1,"红楼梦女子");
        Book book2 = new Book("西游记",2,"西游记师徒");
        Book book3 = new Book("水浒传",3,"水浒传108");

        ArrayList<Book> bookStore = new ArrayList<>();
        bookStore.add(book1);
        bookStore.add(book2);
        bookStore.add(book3);

        Scanner input = new Scanner(System.in);
        String bookName;
        int bookID;

        try {
            while (true) {
                System.out.println("enter number 1(bookID) / 2(bookName) to search book");
                int q = input.nextInt();
                if (q == 1) {
                    System.out.println("enter ID:");
                    bookID = input.nextInt();
                    boolean flag = false;
                    for(Book e:bookStore){
                        if(e.getId() == bookID){
                            System.out.println(e.getContent());
                            flag = true;
                        }
                    }
                    if(!flag){
                        throw new NotexsitException ("图书不存在");
                    }
                } else if (q == 2) {
                    System.out.println("enter the book name:");
                    boolean flag = false;
                    bookName = input.next();
                    for(Book e:bookStore){
                        if(e.getName().equals(bookName)){
                            System.out.println(e.getContent());
                            flag = true;
                        }
                    }
                    if(!flag){
                        throw new NotexsitException("图书不存在");
                    }
                } else if (q == 0) {
                    System.out.println("程序结束");
                    break;
                } else {
                    throw new ErrorQException("错误指令异常");
                }
            }
        }catch(InputMismatchException e){
            System.out.println("数据类型输入错误");
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}